package com.campusbackend.service.impl;

import com.campusbackend.entity.StudentSkills;
import com.campusbackend.entity.vo.StudentSkillVO;
import com.campusbackend.mapper.StudentSkillsMapper;
import com.campusbackend.service.StudentSkillsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 学生技能关联表 服务实现类
 * </p>
 *
 * @author yezi
 * @since 2025-07-24
 */
@Service
public class StudentSkillsServiceImpl extends ServiceImpl<StudentSkillsMapper, StudentSkills> implements StudentSkillsService {

    @Autowired
    private StudentSkillsMapper studentSkillsMapper;

    @Override
    @Transactional
    public boolean saveStudentSkills(Long studentId, List<Long> skillIds) {
        if (studentId == null || skillIds == null || skillIds.isEmpty()) {
            return false;
        }
        
        try {
            int result = studentSkillsMapper.batchInsertStudentSkills(studentId, skillIds);
            return result > 0;
        } catch (Exception e) {
            return false;
        }
    }

    @Override
    @Transactional
    public boolean updateStudentSkills(Long studentId, List<Long> skillIds) {
        if (studentId == null) {
            return false;
        }

        try {
            // 先删除原有的技能关联
            studentSkillsMapper.deleteByStudentId(studentId);

            // 如果技能ID列表不为空，则插入新的技能关联
            if (skillIds != null && !skillIds.isEmpty()) {
                int result = studentSkillsMapper.batchInsertStudentSkills(studentId, skillIds);
                return result > 0;
            }

            // 如果技能ID列表为空，表示清空技能，删除操作已完成，返回true
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    @Override
    public List<StudentSkills> getStudentSkills(Long studentId) {
        if (studentId == null) {
            return null;
        }
        return studentSkillsMapper.getStudentSkills(studentId);
    }

    @Override
    public List<String> getStudentSkillNames(Long studentId) {
        if (studentId == null) {
            return null;
        }
        return studentSkillsMapper.getStudentSkillNames(studentId);
    }

    @Override
    public StudentSkillVO getStudentSkillDetails(Long studentId) {
        if (studentId == null) {
            return null;
        }

        // 获取学生基本信息
        StudentSkillVO studentSkill = studentSkillsMapper.getStudentBasicInfo(studentId);
        if (studentSkill == null) {
            return null;
        }

        // 获取学生的技能详细信息
        List<StudentSkillVO.SkillInfo> skillInfos = studentSkillsMapper.getStudentSkillDetails(studentId);

        // 为每个技能设置熟练程度描述
        if (skillInfos != null) {
            for (StudentSkillVO.SkillInfo skillInfo : skillInfos) {
                skillInfo.setProficiencyLevelDesc(getProficiencyLevelDesc(skillInfo.getProficiencyLevel()));
            }
        }

        studentSkill.setSkills(skillInfos);
        studentSkill.setSkillCount(skillInfos != null ? skillInfos.size() : 0);

        return studentSkill;
    }

    /**
     * 获取熟练程度描述
     * @param level 熟练程度等级
     * @return 熟练程度描述
     */
    private String getProficiencyLevelDesc(Byte level) {
        if (level == null) {
            return "初级";
        }
        switch (level) {
            case 1:
                return "初级";
            case 2:
                return "中级";
            case 3:
                return "高级";
            default:
                return "初级";
        }
    }
}
